using System;
using System.ComponentModel.DataAnnotations;

namespace CR.Domain.Entities
{
    /// <summary>
    /// 用户实体
    /// </summary>
    public class User
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        [Key]
        public int Id { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Required]
        [StringLength(50)]
        public string Username { get; set; }

        /// <summary>
        /// 密码哈希
        /// </summary>
        [Required]
        [StringLength(100)]
        public string PasswordHash { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [StringLength(50)]
        public string RealName { get; set; }

        /// <summary>
        /// 电子邮箱
        /// </summary>
        [StringLength(100)]
        [EmailAddress]
        public string Email { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        [StringLength(20)]
        public string PhoneNumber { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        [StringLength(20)]
        public string Role { get; set; } = "User";

        /// <summary>
        /// 登录失败尝试次数
        /// </summary>
        public int AccessFailedCount { get; set; }

        /// <summary>
        /// 是否锁定
        /// </summary>
        public bool IsLocked { get; set; }

        /// <summary>
        /// 锁定结束时间
        /// </summary>
        public DateTime? LockoutEnd { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 最后登录时间
        /// </summary>
        public DateTime? LastLoginTime { get; set; }

        /// <summary>
        /// 是否激活
        /// </summary>
        public bool IsActive { get; set; } = true;

        /// <summary>
        /// 检查用户是否被锁定
        /// </summary>
        /// <returns>是否被锁定</returns>
        public bool IsLockedOut()
        {
            return IsLocked && (LockoutEnd == null || LockoutEnd > DateTime.Now);
        }

        /// <summary>
        /// 增加登录失败次数，如果超过5次则锁定账号
        /// </summary>
        /// <param name="maxFailedAttempts">最大允许尝试次数</param>
        /// <param name="lockoutMinutes">锁定时间（分钟）</param>
        /// <returns>是否锁定账号</returns>
        public bool IncrementAccessFailedCount(int maxFailedAttempts = 5, int lockoutMinutes = 30)
        {
            AccessFailedCount++;
            
            if (AccessFailedCount >= maxFailedAttempts)
            {
                IsLocked = true;
                LockoutEnd = DateTime.Now.AddMinutes(lockoutMinutes);
                return true;
            }
            
            return false;
        }

        /// <summary>
        /// 重置登录失败次数
        /// </summary>
        public void ResetAccessFailedCount()
        {
            AccessFailedCount = 0;
        }

        /// <summary>
        /// 解除锁定
        /// </summary>
        public void Unlock()
        {
            IsLocked = false;
            LockoutEnd = null;
            AccessFailedCount = 0;
        }
    }
} 